package claybird.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import claybird.dom.Handicap;
import claybird.dom.Result;
import claybird.dom.ShootDay;

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class ShootDayDao extends BaseRepository<ShootDay>{
	
	 public  List<ShootDay> getAllTir(){
	        return em.createQuery("select a from ShootDay a order by a.date").getResultList();
	    }

	public ShootDay findlast() {
		if ( em.createQuery("select s from "+ ShootDay.class.getName()+" s  order by s.date desc")
				.getResultList().size()>0){
			
			return (ShootDay) em.createQuery("select s from "+ ShootDay.class.getName()+" s  order by s.date desc")
					.getResultList().get(0);
		}
		return null;
	}

	public List<ShootDay> getShootByYear(int year) {
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 

		Date datemin = new Date();
		Date datemax = new Date();
		try {
			 datemin = ft.parse(year+"-01-01");
			 datemax = ft.parse(year+"-12-31");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return em.createQuery("select s from "+ ShootDay.class.getName()+" s "
				+" where  s.date > :yearMin and s.date < :yearMax order by s.date")
				.setParameter("yearMin", datemin)
				.setParameter("yearMax", datemax).getResultList();
	}
	 
}
